<?php

namespace app\admin\controller\financing;

use app\common\controller\Backend;
use function fast\e;
use think\Db;
use tool\ExcelUtil;

/**
 * 管理费用录入
 *
 * @icon fa fa-circle-o
 */
class Administrative extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Working;
        $this->model = new \app\admin\model\Book();
    }

    /**
     * 查看
     */
    public function index()
    {

        if(isset(request()->param()['year'])){
            $select_year = request()->param()['year'];
        }else{
            $select_year = date('Y');
        }


        $year_arr = Db::table('fa_administrative')
            ->field('year')
            ->group('year')
            ->select();

        $year_arrs = [];
        if (!empty($year_arr)){
            foreach ($year_arr as $k=>$v){
                $year_arrs[] = $v['year'];
            }
        }

        $years = [date('Y')];
        $year_arrs = array_merge($year_arrs,$years);
        $year_arrss = array_unique($year_arrs);

        $arr = [];
        $arrs = [];
        $name = [];
        $names = [];
        //循环数据
        for ($i=0;$i<=11;$i++){
            for ($j=1;$j<=31;$j++){
                $arr[$j] = Db::table('fa_administrative')
                    ->field('id,money')
                    ->where(['type'=>$j,'month'=>$i+1,'year'=>$select_year])
                    ->find();
                if (!empty($arr[$j])){
                    $name[$j][$i] = $arr[$j]['money'];
                }else{
                    $name[$j][$i] = '0.00';
                }

                $names[$j] = Db::table('fa_administrative')
                    ->where(['type'=>$j,'year'=>$select_year])
                    ->sum('money');

            }

            $arrs[$i] = Db::table('fa_administrative')
                ->where(['month'=>$i+1,'year'=>$select_year])
                ->sum('money');

        }


        $this->view->assign("name", $name);
        $this->view->assign("arrs", $arrs);
        $this->view->assign("names", $names);


        $this->view->assign("year_arr", $year_arrss);
        $this->view->assign("year", date('Y'));
        return $this->view->fetch();
    }


    public function fors(){
        $arr = request()->param();


        if (request()->isPost()){

            if ($arr['money'] == ""){
                $this->error('金额不能为空');
            }

            if ($arr['money'] < 0){
                $this->error('金额不能小于0');
            }

            $pan = Db::table('fa_administrative')
                ->where(['type'=>$arr['row'],'month'=>$arr['name'],'year'=>$arr['year']])
                ->find();

            if (empty($pan)){
                $insert = Db::table('fa_administrative')
                    ->insertGetId([
                        'type' => $arr['row'],
                        'month' => $arr['name'],
                        'year' => $arr['year'],
                        'money' => $arr['money'],
                        'create_time' => time(),
                        'select_time' => strtotime($arr['year'].'-'.$arr['name'].'-15'),
                    ]);
            }else{
                $insert = Db::table('fa_administrative')
                    ->where('id',$pan['id'])
                    ->update([
                        //'type' => $arr['row'],
                        //'month' => $arr['name'],
                        //'year' => $arr['year'],
                        'money' => $arr['money'],
                        'update_time' => time(),
                    ]);
            }

            if ($insert !== false){
                $this->success('成功');
            }else{
                $this->error('失败');
            }


        }


    }


    /**
     * 导出Excel方法
     */
    public function export()
    {
        $year = request()->param()['year'];
        $arr = [];
        $new = [];
        $mo = [
            1 => 0,
            2 => 0,
            3 => 0,
            4 => 0,
            5 => 0,
            6 => 0,
            7 => 0,
            8 => 0,
            9 => 0,
            10 => 0,
            11 => 0,
            12 => 0,
            'names' => '总计',
            'name' => '',
            'sum' => 0,
        ];

        for ($i=1;$i<=31;$i++){
            for ($j=1;$j<=12;$j++){
                $list = Db::table('fa_administrative')
                    ->where(['type'=>$i,'month'=>$j,'year'=>$year])
                    ->find();

                if (!empty($list)){
                    $new[$j] = $list['money'];
                }else{
                    $new[$j] = '0.00';
                }


            }
            $arr[] = $new;
        }

        foreach ($arr as $k=>$v){
            if ($k == 0){
                $arr[$k]['name'] = '办公房租';
                $arr[$k]['names'] = '固定支出';
            }
            if ($k == 1){
                $arr[$k]['name'] = '办公水电等管理杂费';
                $arr[$k]['names'] = '';
            }
            if ($k == 2){
                $arr[$k]['name'] = '员工工资';
                $arr[$k]['names'] = '';
            }
            if ($k == 3){
                $arr[$k]['name'] = '员工保险费';
                $arr[$k]['names'] = '';
            }
            if ($k == 4){
                $arr[$k]['name'] = '员工公积金';
                $arr[$k]['names'] = '';
            }
            if ($k == 5){
                $arr[$k]['name'] = '新办公场所装修';
                $arr[$k]['names'] = '合同支出';
            }
            if ($k == 6){
                $arr[$k]['name'] = '品牌设计';
                $arr[$k]['names'] = '';
            }
            if ($k == 7){
                $arr[$k]['name'] = '办公用品';
                $arr[$k]['names'] = '采购支出';
            }
            if ($k == 8){
                $arr[$k]['name'] = '办公电脑';
                $arr[$k]['names'] = '';
            }
            if ($k == 9){
                $arr[$k]['name'] = '办公费用';
                $arr[$k]['names'] = '';
            }
            if ($k == 10){
                $arr[$k]['name'] = '网络费';
                $arr[$k]['names'] = '';
            }
            if ($k == 11){
                $arr[$k]['name'] = '办公室装修物品采购';
                $arr[$k]['names'] = '';
            }
            if ($k == 12){
                $arr[$k]['name'] = '市场营销等费用';
                $arr[$k]['names'] = '';
            }
            if ($k == 13){
                $arr[$k]['name'] = '药品';
                $arr[$k]['names'] = '';
            }
            if ($k == 14){
                $arr[$k]['name'] = '医用耗材';
                $arr[$k]['names'] = '';
            }
            if ($k == 15){
                $arr[$k]['name'] = '医疗器械';
                $arr[$k]['names'] = '';
            }
            if ($k == 16){
                $arr[$k]['name'] = '办公交通费（市内）';
                $arr[$k]['names'] = '其它支出';
            }
            if ($k == 17){
                $arr[$k]['name'] = '办公差旅费';
                $arr[$k]['names'] = '';
            }
            if ($k == 18){
                $arr[$k]['name'] = '办公运杂费';
                $arr[$k]['names'] = '';
            }
            if ($k == 19){
                $arr[$k]['name'] = '办公招待费';
                $arr[$k]['names'] = '';
            }
            if ($k == 20){
                $arr[$k]['name'] = '教授房租及水电等杂费';
                $arr[$k]['names'] = '';
            }
            if ($k == 21){
                $arr[$k]['name'] = '员工餐费';
                $arr[$k]['names'] = '';
            }
            if ($k == 22){
                $arr[$k]['name'] = '过节费';
                $arr[$k]['names'] = '';
            }
            if ($k == 23){
                $arr[$k]['name'] = '探亲交通费';
                $arr[$k]['names'] = '';
            }
            if ($k == 24){
                $arr[$k]['name'] = '房补';
                $arr[$k]['names'] = '';
            }if ($k == 25){
                $arr[$k]['name'] = '奖金';
                $arr[$k]['names'] = '';
            }
            if ($k == 26){
                $arr[$k]['name'] = '全员健康体检';
                $arr[$k]['names'] = '';
            }
            if ($k == 27){
                $arr[$k]['name'] = '旅游';
                $arr[$k]['names'] = '';
            }
            if ($k == 28){
                $arr[$k]['name'] = '员工活动/拓展';
                $arr[$k]['names'] = '';
            }
            if ($k == 29){
                $arr[$k]['name'] = '年会';
                $arr[$k]['names'] = '';
            }
            if ($k == 30){
                $arr[$k]['name'] = '其它';
                $arr[$k]['names'] = '其它';
            }

            $arr[$k]['sum'] = $v[1]+$v[2]+$v[3]+$v[4]+$v[5]+$v[6]+$v[7]+$v[8]+$v[9]+$v[10]+$v[11]+$v[12];

            foreach ($v as $kk=>$vv){
                $mo[$kk] += $vv;
                $mo['sum'] += $vv;
            }

        }

        $arr[31] = $mo;
        //var_dump($arr);die();

        $excel = new ExcelUtil();
        $dataKV = [
            "names",
            "name",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12",
            "sum",
        ];
        $head = [
            //"序号",
            "费用项目",
            "项目明细",
            "1月",
            "2月",
            "3月",
            "4月",
            "5月",
            "6月",
            "7月",
            "8月",
            "9月",
            "10月",
            "11月",
            "12月",
            "本年累计",
        ];
        $objExcel = $excel->getExcelObj();
        $name = '管理费统计';


        //$objExcel->getActiveSheet()->getStyle("A1:H1")->getFont()->setSize(10)->setBold(true);
        //$objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight("30"); // 设置高
        $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('I')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('J')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('K')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('L')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('M')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('N')->setWidth("25"); //设置宽
        $objExcel->getActiveSheet()->getColumnDimension('O')->setWidth("25"); //设置宽

        $objExcel->getActiveSheet()->mergeCells("A3:A7");
        $objExcel->getActiveSheet()->mergeCells("A8:A9");
        $objExcel->getActiveSheet()->mergeCells("A10:A18");
        $objExcel->getActiveSheet()->mergeCells("A19:A32");
        $objExcel->getActiveSheet()->mergeCells("A34:B34");


        $excel->setFileName($year . $name)
            ->setHead($head)
            ->setTitle($year . $name)
            ->setHasIndex(true)
            ->setDataKV($dataKV)
            ->setData($arr,false);
        $excel->export($objExcel);
    }


}
